const en = {
    msg: {
        greeting: "Hello",
        farewell: "Goodbye"
    }
};

const es = {
    msg: {
        greeting: "Hola",
        farewell: "Adiós"

    }
}
const zhCN = {
    msg: {
        greeting: "你好",
        farewell: "再见"
    }
}

const languages = {
    en,
    es,
    'zh-CN': zhCN
};

const defaultLang = 'zh-CN';

/**
 * 计算时间差
 * @param {*} func 
 * @returns 
 */
function timeDiff(func) {
    const nowStart = Date.now();

    const result = func();

    const nowEnd = Date.now();
    console.log(`i18n 耗时: ${nowEnd - nowStart} ms`);

    return result;
}

/**
 * 
 * @param {*} lang "msg.greeting" "msg.farewell"
 * @returns 
 */
function t(lang) {
    const langs = lang.split('.');
    let langData;
    for (const item of langs) {
        if (!langData) {
            langData = languages[defaultLang];
        }

        if (!langData[item])
            return lang;

        langData = langData[item];
    }
    return langData;
}

console.log(timeDiff(() => t('msg.greeting'))); // Output: Hello